#include "mDataManager.h"


mDataManager::mDataManager(void)
{
    AutoProcessID=0;
    AutoArrowID=0;
}


mDataManager::~mDataManager(void)
{
    QList<ProcessSet>::iterator it;
    for(it=list_Process.begin();it!=list_Process.end();it++)
        delete &it;

    QList<ArrowSet>::iterator it2;
    for(it2=list_Arrow.begin();it2!=list_Arrow.end();it2++)
        delete &it2;
}

bool mDataManager::AddProcess(int ID,QPointF pos,QString text,QColor color)
{

    ProcessSet tProcessSet;
    tProcessSet.processID=ID;
    tProcessSet.processPos=pos;
    tProcessSet.processLabel=text;
    tProcessSet.labelColor=color;


    list_Process.push_back(tProcessSet);
    AutoProcessID++;
    return true;
}

bool mDataManager::DeleteProcess(int id)
{
    int index=0;
    QList<ProcessSet>::iterator it;
    for(it=list_Process.begin();it!=list_Process.end();++it)
    {
        ProcessSet &item=(*it);
        if(item.processID==id)
        {
          list_Process.removeAt(index);
          break;
        }
        index++;
    }
    return true;
}

bool mDataManager::AddArrow(int id,int startID,int endID,QColor color)
{
    AutoArrowID++;
    ArrowSet tArrowSet;
    tArrowSet.arrowID=id;
    tArrowSet.startID=startID;
    tArrowSet.endID=endID;
    tArrowSet.arrowColor=color;
    list_Arrow.push_back(tArrowSet);
    return true;
}

bool mDataManager::DeleteArrow(int id)
{
    int index=0;
    QList<ArrowSet>::iterator it;
    for(it=list_Arrow.begin();it!=list_Arrow.end();++it)
    {
        ArrowSet &item=(*it);
        if(item.arrowID==id)
        {
            list_Arrow.removeAt(index);
            break;
        }
        index++;
    }

    return true;
}

bool mDataManager::SaveFile(QString filepath)
{
    QFile db(filepath);
    if(!db.open(QIODevice::WriteOnly | QIODevice::Truncate)) return false;
    //只写方式打开，并清空以前的信息
    QDomDocument doc;
    QDomElement root = doc.createElement("DiaGramPart");
    doc.appendChild(root); //添加Diagram

    QDomElement TaskItem = doc.createElement("Task");
    QDomText text;

    QList<ProcessSet>::iterator it;

    for(it=list_Process.begin();it!=list_Process.end();++it)
    {
        ProcessSet &diagramSet=(*it);

        QDomElement item = doc.createElement("DiaGram");

        QDomElement itemID = doc.createElement("KeyID");
        QDomElement itemPos = doc.createElement("Position");
        QDomElement itemType = doc.createElement("Type");
        QDomElement itemText = doc.createElement("Text");
        QDomElement itemColor = doc.createElement("TextColor");


        text = doc.createTextNode(QString::number(diagramSet.processID));
        itemID.appendChild(text);
        text = doc.createTextNode(QString::number(diagramSet.processPos.x())+","+QString::number(diagramSet.processPos.y()));
        itemPos.appendChild(text);
        text = doc.createTextNode(diagramSet.processLabel);
        itemText.appendChild(text);

        text = doc.createTextNode("("+QString::number(diagramSet.labelColor.red())+","+QString::number(diagramSet.labelColor.green())+","+QString::number(diagramSet.labelColor.blue())+")");
        itemColor.appendChild(text);

        item.appendChild(itemID);
        item.appendChild(itemPos);
        item.appendChild(itemType);
        item.appendChild(itemText);
        item.appendChild(itemColor);
        TaskItem.appendChild(item);
    }



    root.appendChild(TaskItem);

    //添加Arrow
    QDomElement root2 = doc.createElement("ArrowPart");
    doc.appendChild(root2); //添加Diagram

    QList<ArrowSet>::iterator it2;
    for(it2=list_Arrow.begin();it2!=list_Arrow.end();++it2)
    {
        ArrowSet &arrowSet=(*it2);

        QDomElement arrowItem = doc.createElement("Arrow");
        QDomElement arrowID = doc.createElement("KeyID");
        QDomElement arrowStart = doc.createElement("StartID");
        QDomElement arrowEnd = doc.createElement("EndID");
        QDomElement arrowColor = doc.createElement("Color");


        text = doc.createTextNode(QString::number(arrowSet.arrowID));
        arrowID.appendChild(text);
        text = doc.createTextNode(QString::number(arrowSet.startID));
        arrowStart.appendChild(text);
        text = doc.createTextNode(QString::number(arrowSet.endID));
        arrowEnd.appendChild(text);
        text = doc.createTextNode("("+QString::number(arrowSet.arrowColor.red())+","+QString::number(arrowSet.arrowColor.green())+","+QString::number(arrowSet.arrowColor.blue())+")");
        arrowColor.appendChild(text);

        arrowItem.appendChild(arrowID);
        arrowItem.appendChild(arrowStart);
        arrowItem.appendChild(arrowEnd);
        arrowItem.appendChild(arrowColor);
        root2.appendChild(arrowItem);
    }


    QTextStream out(&db);
    out.setCodec("GB2312");
    doc.save(out,4,QDomNode::EncodingFromTextStream);
    db.close();

    return true;
}
